{extend name="base" /}
{block name="body"}
<link rel="stylesheet" href="/static/dist/basic.min.css" />
<link rel="stylesheet" href="/static/dist/dropzone.min.css" />

<link href="/static/regionCheckBox/regionCheckBox.css" rel="stylesheet" />
<style>
    .layui-tab-content {
        padding: 0;/*layui-tab-content:默认有padding: 10px;的值，因为iframe的绝对定位脱离文档流，所以会存在20px的空白空间*/
    }
    .show-frame {
        top: 50px!important;
        /*默认.layui-layout-admin .layui-body {
            top: 60px;
            bottom: 44px;
        }*/
        overflow: hidden;/*消除浏览器最右边的滚动条*/
    }
    .frame {
        position: absolute;
        padding: 10px;/*与layui-footer隔开一段距离*/
        width: 100%;
        height: 100%;
    }
</style>

<div class="layui-card" style="width: 1000px;max-height:4800px">
    <div class="layui-card-body">
        <form action="{:request()->url()}" class="layui-form" method="post">
            <div class="layui-form-item">
                <label class="layui-form-label">政策名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" value="{$data.name|default=''}" autocomplete="off" placeholder="请输入政策名称" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">选择机构</label>
                <div class="layui-input-block">
                    <select name="aid" lay-verify="required" id="agency" lay-filter="agency">
                        {if condition="!empty($aid)"}
                        <option value="{$aid}">{$aname}</option>
                        {else}
                        {volist name='agency' id='vo'}
                        <option value="{$vo.id}">{$vo.name}</option>
                        {/volist}
                        {/if}
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">选择保险公司</label>
                <div class="layui-input-block">
                    <select name="cid" lay-verify="required" id="company" lay-filter="company">
                        {if condition="!empty($cid)"}
                             <option value="{$cid}">{$cname}</option>
                        {else}
                        {volist name='company' id='vo'}
                        <option value="{$vo.id}">{$vo.name}</option>
                        {/volist}
                        {/if}

                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">选择出单方</label>
                <div class="layui-input-block">
                    <select name="bid" lay-verify="required" id="bill">
                        {if condition="!empty($bid)"}
                        <option value="{$bid}">{$bname}</option>
                        {else}
                        {volist name='bill' id='vo'}
                        <option value="{$vo.id}">{$vo.name}</option>
                        {/volist}
                        {/if}

                    </select>
                </div>
            </div>

            <p style="width: 70px;height: 100px;float: left;padding-top:20px;padding-left:35px;">选择地区</p>
            <div id="regionTest" style="padding-left: 20px;float: left"></div>

            <div class="layui-form-item">
                <label class="layui-form-label">新旧车</label>
                <div class="layui-input-block">
                    <input type="radio" name="car" value="0" title="全部" checked>
                    <input type="radio" name="car" value="1" title="新车" {if condition="isset($data) && $data['car'] == 1"}checked{/if}>
                    <input type="radio" name="car" value="2" title="旧车" {if condition="isset($data) && $data['car'] == 2"}checked{/if}>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">排量</label>
                <div class="layui-input-block">
                    <input type="radio" name="displacement" value="0" title="全部" checked>
                    <input type="radio" name="displacement" value="1" title=">125"  {if condition="isset($data) && $data['displacement'] == 1"}checked{/if}>
                    <input type="radio" name="displacement" value="2" title="<=125"  {if condition="isset($data) && $data['displacement'] == 2"}checked{/if}>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">类型</label>
                <div class="layui-input-block">
                    <input type="radio" name="type" value="0" title="摩托车" checked>
                    <input type="radio" name="type" value="1" title="电动车" {if condition="isset($data) && $data['type'] == 1"}checked{/if}>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">摩托车类型</label>
                <div class="layui-input-block">
                    <input type="radio" name="mototype" value="0" title="全部" {if condition="isset($data) && $data['mototype'] == 0"}checked{/if}>
                    <input type="radio" name="mototype" value="1" title="男装摩托车" {if condition="isset($data) && $data['mototype'] == 1"}checked{/if}>
                    <input type="radio" name="mototype" value="2" title="女装摩托车" {if condition="isset($data) && $data['mototype'] == 1"}checked{/if}>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">商业险结算方式</label>
                <div class="layui-input-block">
                    <select name="commercial_type" lay-verify="required">
                        <option value="0" {if condition="isset($data) && $data.commercial_type == 0"}checked{/if}>保单金额比例(元/单)</option>
                        <option value="1" {if condition="isset($data) && $data.commercial_type == 1"}checked{/if}>每单固定金额(%)</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">商业险佣金值</label>
                <div class="layui-input-block">
                      <input type="text" name="commercial_num" value="{$data.commercial_num|default=''}" autocomplete="off" placeholder="请输入商业险佣金" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">交强险结算方式</label>
                <div class="layui-input-block">
                    <select name="compolsury_type" lay-verify="required">
                        <option value="0" {if condition="isset($data) && $data.compolsury_type == 0"}checked{/if}>保单金额比例(元/单)</option>
                        <option value="1" {if condition="isset($data) && $data.compolsury_type == 1"}checked{/if}>每单固定金额(%)</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">交强险佣金值</label>
                <div class="layui-input-block">
                    <input type="text" name="compolsury_num" value="{$data.compolsury_num|default=''}" autocomplete="off" placeholder="请输入交强险佣金" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">政策开始日期</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" id="test1" name="effect_time" value="{$data.effect_time|default=''}" placeholder="{$data.effect_time|default=''}">
                </div>
             </div>

            <input name="city" value="{$data.city|default=''}" hidden="hidden" id="all">
            <input name="isedit" value="{:input('isedit')}" hidden="hidden">
            {if condition="input('isedit') == 1"}
            <input value="{$data.id}" hidden="hidden" name="id">
            {/if}
        </form>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-filter="i" id="sub" >保存</button>
            </div>
        </div>

    </div>
</div>

<script type="text/javascript" src="/static/js/jquery.min.js"></script>
<script src="/static/js/clipboard.js"></script>
<script src="/static/dist/dropzone.min.js"></script>
<script type="text/javascript" charset="utf-8" src="/static/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="/static/ueditor/ueditor.all.js"> </script >

{/block}

{block name="js"}
<script type="text/javascript" src="/static/js/layui.base.js"></script>

<script>
    $(function () {
        layerresize();
    });


    layui.config({
        base: '/static/' //配置组件存放的基础目录
    }).extend({
        regionCheckBox: 'regionCheckBox/regionCheckBox' //定义组件模块名
    }).use(['regionCheckBox'], function(){
        var $ = layui.$;
        var regionCheckBox = layui.regionCheckBox;

        var arr = $('#all').val().split(',');

        //执行实例
        regionCheckBox.render({
            elem: '#regionTest',
            name: 'region', //input name
            value: arr, //赋初始值
            width: '550px', //默认550px
            border: true, //默认true
            ready: function(){ //初始化完成时执行

            },
            change: function(result){ //点击复选框时执行
                $('#all').val(getAllChecked());
            }
        });

        function getAllChecked(){
            var all = '';
            $("input:checkbox[name='region']:checked").each(function(){
                all += $(this).attr('title') + ',';
            });
            return all.substring(0, all.length-1);
        }

    });

    $('#sub').click(function(){
        if($('input[name="name"]').val() == ''){
            layer.open({
                content: '政策名称必填' //这里content是一个普通的String
            });
            return;
        }

        if($('input[name="compolsury_num"]').val() == ''){
            layer.open({
                content: '交强险佣金值必填' //这里content是一个普通的String
            });
            return;
        }


        if($('input[name="commercial_num"]').val() == ''){
            layer.open({
                content: '商业险佣金值必填' //这里content是一个普通的String
            });
            return;
        }

        if($('input[name="city"]').val() == ''){
            layer.open({
                content: '地区必选' //这里content是一个普通的String
            });
            return;
        }

        if($('input[name="effect_time"]').val() == ''){
            layer.open({
                content: '政策开始日期必选' //这里content是一个普通的String
            });
            return;
        }
        var data = $('form').serializeJson();
        $.ajax({
            url:'policy_add',
            data: data,
            method:'post',
            success:function(res){
                layer.open({
                    content: res.msg, //这里content是一个普通的String
                    end: function(){ //此处用于演示
                        if(res.code == 1){
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.location.reload();//刷新父页面，注意一定要在关闭当前iframe层之前执行刷新
                            parent.layer.close(index); //再执行关闭
                        }
                    }
                });
            }
        });
    })



    layui.use('form', function(){
        var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功

        form.on('select(agency)', function(data){
            $.ajax({
                url:'getCompany',
                data:{'id':data.value},
                method:'post',
                success:function(res){
                    $('#company').children().remove();
                    let data = JSON.parse(res);
                    let str = '';
                    for(let i in data){
                        str += ' <option value="'+data[i]['id']+'">'+data[i]['name']+'</option>'
                    }
                    $('#company').append(str);
                    form.render();
                }
            });
        });

        form.on('select(company)', function(data){
            $.ajax({
                url:'getBill',
                data:{'id':data.value},
                method:'post',
                success:function(res){
                    $('#bill').children().remove();
                    let data = JSON.parse(res);
                    let str = '';
                    for(let i in data){
                        str += ' <option value="'+data[i]['id']+'">'+data[i]['name']+'</option>'
                    }
                    $('#bill').append(str);
                    form.render();
                }
            });
        });
    });

    layui.use('laydate', function(){
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#test1' //指定元素
        });

    });
</script>
{/block}
